package me

import grails.gorm.transactions.Transactional
import groovy.sql.Sql

@Transactional
class StartupService {
	def dataSource

	def parseOption = {hm,it->
    	hm.id = it.pkid
    	hm.text = it.name
    	hm.jsonStr = it.json_str
    	hm.optionType = "string"
	    if (it.type=="\"number\"") {
			hm.optionType = "number"
		}
		if (it.type=="\"boolean\"") {
			hm.optionType = "boolean"
		}
		if (it.type=="\"Color\"") {
			hm.optionType = "color"
		}
		hm.optionName = it.name
		hm.seq = it.seq
	    hm.optionId = it.ywname
	    hm.optionJson = it.json_str
	    hm.optionValue = (it.value!=null)?it.value.replace("'","").replace("\"",""):""
	    hm.detailFlag = it.detail_flag

	}
 
 
	def getconfigs = {
		println("begin get configs")
    	def sql = new Sql(dataSource)
   		def results = [success:true]
    	def options = sql.rows(""" SELECT * FROM base_attrs_info where parent_id = ''  order by seq""")
    	Util.chartOption = [:]
		options.each{
			def childrens = []
			def hm = [:]
			parseOption(hm,it)
			def rows = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it.pkid)
    		rows.each{it2->
    			def childrens2 = []
    			def hm2 = [:]
    			parseOption(hm2,it2)
	    		def rows2 = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it2.pkid)
		    	rows2.each{it3->
		    		def childrens3 = []
		    		def hm3 = [:]
		    		parseOption(hm3,it3)
		    		def rows3 = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it3.pkid)
			    	rows3.each{it4->
			    		def childrens4 = []
			    		def hm4 = [:]
			    		parseOption(hm4,it4)
			    		def rows4 = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it4.pkid)
				    	rows4.each{it5->
				    		def childrens5 = []
				    		def hm5 = [:]
				    		parseOption(hm5,it5)
				    		def rows5 = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it5.pkid)
					    	if (rows5.size()==0) {
								hm5.leaf = true
							} else {
								hm5.leaf = false
								hm5.children = childrens5
							}
							childrens4.add(hm5)
							Util.chartOption[hm5.id] = hm5
				    	}
						if (rows4.size()==0) {
							hm4.leaf = true
						} else {
							hm4.leaf = false
							hm4.children = childrens4
						}
						childrens3.add(hm4)
						Util.chartOption[hm4.id] = hm4
			    	}
					if (rows3.size()==0) {
			    		hm3.leaf = true
			    	} else {
			    		hm3.leaf = false
			    		hm3.children = childrens3
			    	}
			    	childrens2.add(hm3)
			    	Util.chartOption[hm3.id] = hm3
			    }
				if (rows2.size()==0) {
		    		hm2.leaf = true
		    	} else {
		    		hm2.leaf = false
		    		hm2.children = childrens2
		    	}
			    childrens.add(hm2)
			    Util.chartOption[hm2.id] = hm2
			}
	    	if (rows.size()==0) {
	    		hm.leaf = true
	    	} else {
	    		hm.leaf = false
	    		hm.children = childrens
	    	}
	    	Util.chartOption[hm.id] = hm

	    	if (hm.leaf) {
		    	if (Util.chartOption["root"]==null) {
		    		Util.chartOption["root"] = [id:'root',leaf:false,children:[]]
		    		Util.chartOption["root"].children.add(hm)
		    	} else {
		    		Util.chartOption["root"].children.add(hm)
		    	}
	    	}

   		}
   		println("end get configs")
   		
    }

	def getchartband = {
		println("begin getchartband")
    	def results = [success:true]
    	Util.chartBand = [
    		[id:"dataSource",text:"数据源",leaf:true,,iconCls:'x-fa fa-database categoryCls'],
    		[id:"chartInstance",text:"图表实例",leaf:true,,iconCls:'x-fa fa-window-restore categoryCls' ]
    	]


    	def sql = new Sql(dataSource)
   			  
    	def options = sql.rows(""" SELECT * FROM base_attrs_info where parent_id = '' order by seq""")
    	def chartCategory = []
		options.each{
			def childrens = []
			def hm = [:]
			hm.id = "category"+it.pkid
			hm.jsonStr = it.json_str
			hm.text = it.name+" <font color=blue>"+it.ywname+"</font>"
			hm.iconCls = "x-fa fa-th-list categoryCls"
			hm.detailFlag = it.detail_flag
			def rows = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it.pkid)
    		rows.each{it2->
    			def childrens2 = []
    			def hm2 = [:]
    			hm2.id = "category"+it2.pkid
				hm2.jsonStr = it2.json_str
	    		hm2.text = it2.name+" <font color=blue>"+it2.ywname+"</font>"
	    		hm2.iconCls = "x-fa fa-th-list categoryCls"
	    		hm2.detailFlag = it2.detail_flag
	    		def rows2 = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it2.pkid)
		    	rows2.each{it3->
		    		def childrens3 = []
		    		def hm3 = [:]
		    		hm3.id = "category"+it3.pkid
					hm3.jsonStr = it3.json_str
	    			hm3.text = it3.name+" <font color=blue>"+it3.ywname+"</font>"
	    			hm3.iconCls = "x-fa fa-th-list categoryCls"
	    			hm3.detailFlag = it3.detail_flag
		    		def rows3 = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it3.pkid)
			    	rows3.each{it4->
			    		def childrens4 = []
			    		def hm4 = [:]
			    		hm4.id = "category"+it4.pkid
						hm4.jsonStr = it4.json_str
	    				hm4.text = it4.name+" <font color=blue>"+it4.ywname+"</font>"
	    				hm4.iconCls = "x-fa fa-th-list categoryCls"
	    				hm4.detailFlag = it4.detail_flag
			    		def rows4 = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it4.pkid)
				    	rows4.each{it5->
				    		def childrens5 = []
				    		def hm5 = [:]
				    		hm5.id = "category"+it5.pkid
							hm5.jsonStr = it5.json_str
	    					hm5.text = it5.name+" <font color=blue>"+it5.ywname+"</font>"
	    					hm5.iconCls = "x-fa fa-th-list categoryCls"
	    					hm5.detailFlag = it5.detail_flag
				    		def rows5 = sql.rows(" SELECT * FROM base_attrs_info where parent_id = ?  order by seq",it5.pkid)
					    	if (rows5.size()==0) {
								hm5.leaf = true
							} else {
								hm5.leaf = childrens5.size()!=0?false:true
								hm5.children = childrens5
								childrens4.add(hm5)
							}
							
				    	}
						if (rows4.size()==0) {
							hm4.leaf = true
						} else {
							hm4.leaf = childrens4.size()!=0?false:true
							hm4.children = childrens4
							childrens3.add(hm4)
						}
						
			    	}
					if (rows3.size()==0) {
			    		hm3.leaf = true
			    	} else {
			    		hm3.leaf = childrens3.size()!=0?false:true
			    		hm3.children = childrens3
			    		childrens2.add(hm3)
			    	}
			    	
			    }
				if (rows2.size()==0) {
		    		hm2.leaf = true
		    	} else {
		    		hm2.leaf = childrens2.size()!=0?false:true
		    		hm2.children = childrens2
		    		childrens.add(hm2)
		    	}
			    
	    	}
	    	if (rows.size()==0) {
	    		hm.leaf = true
	    	} else {
	    		hm.leaf = childrens.size()!=0?false:true
	    		hm.children = childrens
	    		chartCategory.add(hm)
	    	}
	    

   		}

   		Util.chartBand.add(
   			[id:'categoryroot',text:'配置项',leaf:false,children:chartCategory,iconCls:'x-fa fa-cubes categoryCls']
   		)

   		Util.chartBand.add(
   			[id:'chartSource',text:'图表源码',leaf:true,iconCls:'x-fa fa-code categoryCls']
   		)
   		Util.chartBand.add(
   			[id:'chartTheme',text:'主题',leaf:true,iconCls:'x-fa fa-paint-brush categoryCls']
   		)
   		Util.chartBand.add(
   			[id:'chartEvent',text:'事件',leaf:true,iconCls:'x-fa fa-flag categoryCls']
   		)
   		println("end getchartband")	
    }	

    def init() {
    	getconfigs()
    	getchartband()
		getViewChartCache()

    }

	def getViewChartCache() {
		def sql = new Sql(dataSource)
		sql.rows("select * from chart_file").each{
			Util.cacheChartFile[it.token] = it
		}

		sql.rows("select * from chart_theme").each{
			Util.cacheChartTheme[it.id+""] = it
		}

		sql.rows("select * from data_source_hy_info").each{
			Util.cacheChartDataSource[it.id+""] = it
		}

		sql.rows("select * from chart_param").each{
			if (Util.cacheChartParam[it.chart_id+""]==null) {
				Util.cacheChartParam[it.chart_id+""] = [it]
			} else {
				Util.cacheChartParam[it.chart_id+""].add(it)
			}
		}

		sql.rows("select * from remote_adapter").each{
			Util.cacheChartAdapter[it.id+""] = it
		}

		sql.rows("select * from table_theme").each{
			Util.cacheChartTableTheme[it.id+""] = it
		}


	}


}
